
var code_err_msg = "输入接口描述";

$(function() {
    /*表单验证：begin*/
    var $myField = null;
    var myMsg = "";
    $("#frm").validator({
        // 域通过验证时回调
        onValid: function (validity) {
            $(validity.field).closest('.am-form-group').find('.am-alert').hide();
        },
        // 验证出错时的回调， validity 对象包含相关信息，格式通 H5 表单元素的 validity 属性
        onInValid: function (validity) {
            var $field = $(validity.field);
            var field_id = $field.attr('id');

            var $group = $field.closest('.am-form-group');
            var $alert = $group.find('.am-alert');
            var msg = "";
            if ($myField && myMsg && $field.val() && $field.is($myField)) {
                msg = myMsg;
                $myField = null;
                myMsg = "";
            }else if(field_id=="api"){
                msg = code_err_msg;
            }else {
                // 使用自定义的提示信息 或 插件内置的提示信息
                msg = $field.data('validationMessage') || this.getValidationMessage(validity);
            }
            if (!$alert.length) {
                $alert = $("<div class='am-alert am-alert-danger'></div>").hide().
                appendTo($group.find(".input-msg"));
            }
            console.log("onInValid : " + $field.val());
            $alert.html(msg).show();
        },
        //自定义验证
        validate: function (validity) {
            if ($(validity.field).is('#api')) {
                var api = $("#api").val().trim();
                if(!api || api.length<2){
                    validity.valid = false;
                }
            };


            /**
             * 校验地市资费代码的唯一性
             */
            var validityApiDescribesUnique = function(){
                var api = $("#api").val();
                var apiDescribeId = $("#apiDescribeId").val();
                var params = {api:api,apiDescribeId:apiDescribeId};
                if(api && api.trim().length>0){
                    $.ajax({
                        type: "post",
                        url: "../transactionLog/validityApiDescribesUnique",
                        data: params,
                        dataType: "json",
                        async:false,
                        success: function(data) {
                            if(data.resultCode != 1000){
                                code_err_msg = "接口名称已存在";
                                validity.valid = false;
                            }else{
                                validity.valid = true;
                            }
                        },
                        error : function(textStatus) {
                            validity.valid = false;
                        }
                    });
                }else{
                    code_err_msg = "输入接口名称";
                    validity.valid = false;
                }
            };
            if ($(validity.field).is('#api')) {
                validityApiDescribesUnique();
            };
        },
        submit: function () {
            var formValidity = this.isFormValid();
            // 表单验证成功，弹出确认提示
            if (formValidity) {
                return true;
            }
            console.warn('验证状态：', formValidity ? '通过' : '未通过');
            return false;
        }
    });

});
